<?php

/**
 * Well Framework
 * 
 * Layout Class File
 * 
 * Layout object contain main view
 * & wrapper currently command view in it own.
 * 
 * @todo finish with layout headers |   have done   |   take a look on it's working
 * 
 * @category   
 * @package    Well
 * @subpackage 
 * 
 * @author valeriy lazarevych <valeriy.fremar@gmail.com>
 */

/**
 * Layout Well Framework Class
 */
class Layout
{
    /**
     * Placeholder for layout file name
     * 
     * @var type string
     */
    protected $layout          =   'front_end';
    
    /**
     * Extenstions of layout filename
     * By default = 'phtml'
     * 
     * @var string 
     */
    protected $layout_suffix   =   '.phtml';
    
    /**
     * Path of layout files placeholder
     * 
     * @var type string = 'layouts/scripts/'
     */
    protected $layouts_path    =   'Layouts/scripts/';
    
    /**
     * Path of layout helper
     * 
     * @var type string
     */
    protected $helpers_path    =   'Layouts/helpers/scripts';
    
    /**
     * Layout Helper class name suffix
     * Add to helper class name
     * 
     * @var type string
     */
    protected $helper_suffix   =   '_LayoutHelper';
    
    /**
     * Html page doc type
     * 
     * @var string $_doctype
     */
    protected $_doctype        =   '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
    
    /**
     * Array of html page headers 
     * @var array $headers 
     */
    protected $headers         =   array();

    /**
     * Contain html view of command
     * 
     * @var type string
     */
    public $command_view    =    null;      

    
    /**
     * Put $view to $_view
     * 
     * @param string $view
     */
    public function __construct(View $view = null)
    {                                    
        if (!$view) {
             $view =  new View('index');
        }        
        $this->setCommandView($view);           
    }                  
    
    /**
     * Adding helpers obj
     */
    private function _init()
    {
    }
    
    /**
     * Return layout name
     * 
     * @return type string file name
     */
    public function getLayout()
    {
        return  $this->layout;
    }
    
    /**
     * Set layout
     * 
     * @param string $layout_name
     */
    public function setLayout($layout_name)
    {
        if ( file_exists($this->layouts_path. $layout_name. $this->layout_suffix) ){
            
            $this->layout  =   (string)$layout_name;
        }
    }
    
    /**
     * Include layout
     */
    public function render()
    {                        
        require_once $this->layouts_path. $this->layout. $this->layout_suffix;                
    }
    
    /**
     * Include command View script into current layout 
     */
    protected function layoutContent()
    {
        $this->command_view->render();        
    }
    
    /**
     * Add command view to layout
     *      
     * @param string $view
     */
    public function setCommandView(View $view)
    {
        $this->command_view    =   $view instanceof View    ?   $view   :   $this->command_view;
    }
    
    /**
     * Instanceate a helper obj
     * 
     * @todo rebuild helper instanciation
     * 
     * @param string $helper_name 
     */
    public function getHelper($helper_name)
    {                   
        $class_name =   $helper_name . $this->helper_suffix;        
        
        // try to make instance of helper obj        
        include 'layouts/helpers/scripts/'.$helper_name.'.php';        
        $helper     =   new $class_name($helper_name);       
        
        return $helper;        
    }
    
    /*
     * HTML PAGE HEADER FUNCTIONS OPERATION
     */
    
    /**
     * Set doc type line at html page
     * 
     * @param string $doctype 
     */
    public function docType( $doctype = null )
    {        
        if ($doctype)
            $this->_doctype    =   (string)$doctype;
        
        return $this->_doctype;
    }
    
    /**
     * Add head title
     * @param string $title 
     */
    public function Title($title = null)
    {
        if ($title)
            $this->headers['title'] =  '<title>'. $title.'</title>';
        
        return isset($this->headers['title'])   ?  $this->headers['title'] : 'Page title';
    }
    
    /**
     * Add javascript include by link
     * @param string $url 
     */
    public function ScriptLink($url = null)
    {
        if ($url)
            $this->headers['script'][] =  '<script type="text/javascript" src="'. $url .'"></script>';
        
        return isset($this->headers['script'])  ?  $this->headers['script']  :  array();
    }
    
    /**
     * Add javascript block
     * @param string $script 
     */
    public function Script($script = null)
    {
        if ($script)
            $this->headers['scriptBlocks'][] =  '<script type="text/javascript">'.$script.'</script>';
        
        return isset($this->headers['scriptBlocks'])  ?  $this->headers['scriptBlocks'] :  array(); 
    }
    
    /**
     * Add meta link
     * 
     * @param string $type
     * @param string $rel
     * @param string $url 
     */
    public function Link($type = null, $rel = null, $url = null)
    {        
        if ($url && $type)
            $this->headers['link'][] =  '<link href="'.$url .'" type="'.$type .'" rel="'.$rel .'"/>';
        
        return isset($this->headers['link'])   ?  $this->headers['link'] :  array();
    }
    
    /**
     * Add meta http-equiv line
     * 
     * @param string $type
     * @param string $content 
     */
    public function HttpEquiv($type = null, $content = null)
    {
        if ($content && $type)
            $this->headers['meta']['http-equiv'] =  '<meta http-equiv="'.$type.'" content="'.$content.'" />';
        
        return isset($this->headers['meta']['http-equiv'])  ?  $this->headers['meta']['http-equiv'] :  'DEFAULT VALUE';
    }
    
    /**
     * Add head meta line
     * 
     * @param string $name
     * @param string $content 
     */
    public function Meta($name = null, $content = null)
    {
        if ($content && $name)            
            $this->headers['meta'][$name] =  '<meta name="'.$name.'" content="'.$content.'" />';
        
        return isset($this->headers['meta'])    ?  $this->headers['meta'] :  array();
    }
    
    /**
     * Add page meta description
     * @param string $desc
     */
    public function Description($desc = null)
    {
        if ($desc)
            $this->Meta('description', $desc);
        
        return isset($this->headers['meta']['description'])   ?  $this->headers['meta']['description'] :  'default meta descriptions';
    }    
    
    /**
     * Add page meta keywords
     * @param string $keywords
     */
    public function Keywords($keywords = null)
    {
        if ($keywords)
            $this->Meta('keywords', $keywords);
        
        return isset($this->headers['meta']['keywords'])      ?  $this->headers['meta']['keywords'] :  'default keywords';
    }        
}
?>
